package com.appcenter.application.mapper;


import com.appcenter.application.input.Dic.GetDicsByKeysInput;
import com.appcenter.application.input.Dic.GetDicsInput;
import com.appcenter.application.model.Dics;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface DicsMapper extends Mapper<Dics> {

    @Select("<script>" +
            "select * from dics where 1=1 " +
            "" +
            " <when test='keyword!=null'>" +
            " AND (DicKey like concat('%',#{keyword},'%') or DicCode like concat('%',#{keyword},'%') or DicName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " <when test='AppId!=null'>" +
            " AND AppId=#{AppId}" +
            " </when>" +
            "" +
            "" +
            "</script>")
    public List<Dics> searchDicList(GetDicsInput input);


    //查询当前应用下字典主键是否存在
    @Select("select * from dics where AppId= #{AppId} AND DicKey= #{DicPrm}")
    public List<Dics> getPrmCount(@Param("AppId") String AppId,@Param("DicPrm") String DicPrm);

    //查询当前应用下字典主键是否存在
    @Select("select * from dics where AppId= #{AppId} AND DicKey= #{DicPrm} and id <> #{Id}")
    public List<Dics> getPrmCountNoId(@Param("AppId")String AppId,@Param("DicPrm")String DicPrm,@Param("Id") String Id);




    //查询当前应用下字典编码是否存在
    @Select("select * from dics where AppId=#{AppId} AND ParentId=#{ParentId} AND DicCode=#{DicCode}")
    public List<Dics> getCodeCount(@Param("AppId") String AppId,@Param("DicCode") String DicCode, @Param("ParentId") String ParentId);

    //查询当前应用下字典编码是否存在
    @Select("select * from dics where AppId=#{AppId} AND ParentId=#{ParentId} AND DicCode=#{DicCode} and id <> #{Id} ")
    public List<Dics> getCodeCountNoId(@Param("AppId") String AppId,@Param("DicCode") String DicCode,@Param("ParentId") String ParentId,@Param("Id") String Id);


    //查询当前应用下字典值是否存在
    @Select("select * from dics where AppId=#{AppId} AND ParentId=#{ParentId} AND DicName=#{DicVal}")
    public List<Dics> getValCount(@Param("AppId") String AppId,@Param("DicVal") String DicVal,@Param("ParentId") String ParentId);

    //查询当前应用下字典值是否存在
    @Select("select * from dics where AppId=#{AppId} AND ParentId=#{ParentId} AND DicName=#{DicVal} and id <> #{Id} ")
    public List<Dics> getValCountNoId(@Param("AppId") String AppId,@Param("DicVal") String DicVal,@Param("ParentId") String ParentId,@Param("Id") String Id);


    //查询应用中最大的ListOrder

    @Select("select * from dics WHERE AppId=#{AppId} AND ParentId=#{ParentId} ORDER BY ListOrder DESC limit 0,1")
    public Dics maxListOrderDic(@Param("AppId") String AppId,@Param("ParentId") String ParentId);

    //字典查询
    @Select("select dics.*  from dics  inner join apps on dics.AppId=apps.Id where AppCode=#{AppCode}  AND  dics.DicKey=#{DicKey}")
    public Dics getDicsByKey(@Param("AppCode") String AppCode,@Param("DicKey") String DicKey);

    @Select("select *  from dics  WHERE  ParentId=#{ParentId}")
    public List<Dics> getChildrenByParentId(@Param("ParentId") String ParentId);


}
